#include <iostream>
#include <string>
#include <math.h>
#include <fstream>
#include <algorithm>
#include <stdio.h>
#include <boost/filesystem/path.hpp>
#include <boost/program_options.hpp>
#include "get_arg.h"



// link with -lboost_filesystem-gcc -lboost_program_options-gcc
// program_options_helper.cpp
//-lboost_filesystem-gcc-d
//-L/usr/local/lib





// my functions
#include <print_error.h>
#include <program_options_helper.h>
#include <stlhelper.h>
#define DEBUG_LEVEL 6
#include <debug_func.h>
#include <blast_results.h>
#include <codeml_results.h>

using std::string;
using std::cerr;
using std::string;
using std::ofstream;
using std::ifstream;
using std::cerr;
using std::cout;
using std::max;
using boost::filesystem::path;
#define DEFAULT_CODEML_PATH "/net/cpp-group/tools/paml/"
using namespace program_options_helper;
namespace po = boost::program_options_lg;


//_______________________________________________________________________________________

//		process_command_line_options

//				from command line
//				Created [9/13/2001]

//_______________________________________________________________________________________
bool process_command_line_options(int argc, char* argv[], t_program_args& args)
{

	//
	// checked mandatory arguments
	//
	po::options_description mandatory_args("Mandatory Arguments");
	mandatory_args.add_options()
	("map_identifiers,m",	po::value<string>(&args.file_map)
									->set_name("FILE"),
								"Contains sequence identifiers in "
								"tab-delimited columns (extra columns will be ignored.");



	args.optional_args.add_options()
	("input_file,i", 			po::value<string>(&args.file_input)
										->set_name("FILE")
										->default_value("STDIN"),
									"Contains sequence identifiers in the first two "
									"tab-delimited columns.")
	("from_format,f",			po::value<string>(&args.from_format)
										->default_value(":0")
										->set_name("FIELDS"),
									"Fields containing matching identifiers to be "
									"looked up.")
	("to_format,t",				po::value<string>(&args.to_format)
											->default_value(":1")
											->set_name("FIELDS"),
									 "Fields containing substitute identifiers "
									 "for matches.")
	("First_instance,F",		po::bool_switch(&args.first_instance)
											->default_value(false),
									"Ignore the second instance of an identifier in the "
									"map. All identifiers map 1->1")
	("Ignore_duplicates,I",		po::bool_switch(&args.ignore_duplicates)
											->default_value(false),
									"Ignore all identifiers which map to more than one "
									"result. Only keep identifiers which map 1->1")
	("keep_non_matching_lines,k",po::bool_switch(&args.keep_non_matching_lines)
											->default_value(false),
									"Write through all lines where one or both identifiers "
									"do not have a map entry.")
	("Unmatched_identifiers,U",	po::value<string>(&args.file_unmatched_identifiers)
									->set_name("FILE"),
									"Will contain a list of identifiers found in the "
									"identifier pairs which are not in the map.")
	("#comment,#",				po::value<string>(&args.comment_letters)
										->set_name("LETTERS")
										->default_value("#"),
									"Use one of these letters to begin a "
									"commented line.");
	


	args.add_std_options();
	po::options_description all_args("");
	all_args
				.add(mandatory_args)
				.add(args.optional_args);

	// parse command line
	po::variables_map vm;
	po::store(po::command_line_parser(argc, argv).options(all_args).run(), vm);
	po::notify(vm);

	if (vm.count("version"))
	{
		print_version(cerr, *argv, "1.4\n"
			"\tHistory: \n\n"
			"\tv 1.0"
			"22-08-2005\1"
			"First Version\1"
			"For use in orthology pipeline.\n"
			"\tv 1.1\1"
			"04-01-2006\1"
			"For Andreas: Hash start comment lines. Fix bugs in handling input. "
			"Upgrade to common code for sequence pair identifiers\n"
			"\tv 1.2\1"
			"23-03-2006\1"
			"Added --Unmatched_identifiers option to save unmatched identifers to a file "
			"rather than just be dumped on the command line."
			"\tv 1.3\1"
			"27-03-2006\1"
			"Comments can begin with user-defined letters. Defaults to hash '#'\n"
			"\tv 1.4\1"
			"27-03-2006\1"
			"Allow any column to be the source and destination identifiers in the map file.\n"
			"\tv 1.5\1"
			"24-11-2006\1"
			"Allow more flexible handling of columns in the map file.\n"

			);
		return false;
	}
	if (vm.count("help"))
	{
		string exe_description =	"Takes sequence pair results and substitutes new "
									"identifiers given a tab-delimited map file.\n"
									"Substitutions can be from identifiers derived from "
									"any column of the map file to another.\n"
									"Columns are specified using (0-based) colon ':' "
									"notation .\n"
									"Thus -f ':0_:1' -t ':2' would map the first two "
									"columns, joined by an underscore, to "
									"the third column.\n"
									"A colon (the letter ':') can be specified "
									"using '::'.\n"
									"For maximum flexibility, columns can appear in any "
									"order and any number of times: "
									"e.g. -f 'gene_:0^id:4:0' -t 'try_:0'.\n"
									"Normally if there is a 1->many mapping, each "
									"matching line will be duplicated accordingly:\1"
									"E.g. if 'A' -> 'a' and also 'A' -> 'b', and 'C' -> 'c', "
									"then the single line:\1"
									"       A  C ...       will produce 2 resulting lines\1"
									"=>     a  c ...       and\1"
									"=>     b  c ...       \n"
									"Specify --First_instance or --Ignore_duplicates "
									"if this is not appropriate.\n"
									"Lines with identifiers not in the map file will be "
									"discarded unless --keep_non_matching_lines is "
									"specified.";

		print_usage(cerr, string(*argv), "[OPTIONS]\n"
										 "--map_identifiers IDS.MAP\n"
										 "--input_file SEQUENCE_PAIR.RESULTS ",
							all_args, exe_description, 80);
		return false;
	}
	check_required_options(vm, mandatory_args);

	args.open_err_log();
	return true;
}

